﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TrailMapManager.Communication;

namespace TrailMapManager.Common.Poi
{
    public class PoiManager : BaseViewLogic
    {
        private static PoiManager _instance;
        public static PoiManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PoiManager();
                }
                return _instance;
            }
        }

        private ObservableCollection<PoiPoint> _points;
        public ObservableCollection<PoiPoint> Points
        {
            get
            {
                if (_points == null)
                {
                    _points = new ObservableCollection<PoiPoint>();
                    _points.CollectionChanged +=
                        (sender, args) =>
                        {
                            NotifyPropertyChanged("PoiCount");
                        };
                }
                return _points;
            }
        }

        public int PoiCount
        {
            get
            {
                if (Points != null)
                {
                    return Points.Count;
                }
                return 0;
            }
        }

        public void SavePoiToPhone(PoiPoint newPoi)
        {
            CommunicationManager.Instance.AddNewCommandToQueue( new DownloadTrailPoiCommand()
            {
                Poi = new TrailPoi()
                {
                    Name = newPoi.Name,
                    Position = new TrailCoordinate()
                    {
                        Latitude = (float)newPoi.Position.Latitude,
                        Longitude = (float)newPoi.Position.Longitude,
                    },
                    Description = newPoi.Description,
                    Id = newPoi.Id,
                },
                Type = CommandType.DownloadPoi,
            });
        }

        internal void AddPoint(TrailPoi pois)
        {
            var newPoi = new PoiPoint()
            {
                Description = pois.Description,
                Id = pois.Id,
                Name = pois.Name,
                Position = new Microsoft.Maps.MapControl.WPF.Location(pois.Position.Latitude, pois.Position.Longitude),
            };
            if (!Points.Contains(newPoi))
            {
                Points.Add(newPoi);
            }
            
        }

        internal void ClearAll()
        {
            Points.Clear();
        }

        public void DeltePoi(PoiPoint poi)
        {
            CommunicationManager.Instance.AddNewCommandToQueue(new Communication.DeleteTrailPoiCommand()
            {
                Poi = new Communication.TrailPoi()
                {
                    Id = poi.Id,
                    Name = poi.Name,
                },
                Type = Communication.CommandType.DeletePoi,
            });

            this.ClearAll();
            CommunicationManager.Instance.AddNewCommandToQueue(new TrailCommand()
            {
                Type = CommandType.GetPoiList,
            });
        }
    }
}
